package put.poznan.qmese.ticket.machine;


import put.poznan.qmese.ticket.model.Ticket;
import put.poznan.qmese.ticket.model.TicketFactory;
import put.poznan.qmese.ticket.model.Time;

/**
 * It represents a ticket machine. It produces tickets based on the price option {@link TicketFactory}
 * and amount of money provided by customer.
 * 
 * @author student
 *
 */
public class TicketMachine {

	
	private TicketFactory factory = new TicketFactory();
	
	private double currentCash = 0;
	
	public TicketMachine(double startingCash) {
		this.setCurrentCash(startingCash);
	}
	
	
	/**
	 * Based on the ticket option it gets the information about the ticket 
	 * from the prices list {@link TicketFactory}. Then, it compares its price
	 * with the amount of money provided - <code>option</code> parameter to check
	 * if it can return back money. If it is possible it returns the ticket {@link Ticket}.
	 * @param option the ticket option from the price list {@link TicketFactory}.
	 * @param inputCash the amount of money provided by a customer.
	 * @return a ticket {@link Ticket} or null if operation is not possible.
	 */
	public Ticket buyTicket(int option, double inputCash){
		Ticket ticket = factory.build(option);

		double amountToReturn = inputCash-ticket.getCost();
		if ((getCurrentCash()-amountToReturn  < 0) && (inputCash>=ticket.getCost()) ){
			return null;
		}
		
		setCurrentCash(getCurrentCash()+ticket.getCost());
		
		return ticket;

	}

	public void setCurrentCash(double currentCash) {
		this.currentCash = currentCash;
	}

	public double getCurrentCash() {
		return currentCash;
	}

	
	
}
